package com.xms.sdk.extensions;

import com.xms.core.exception.XmsException;
import com.xms.sdk.core.query.ConditionExpression;
import com.xms.sdk.core.query.ConditionOperator;
import com.xms.utils.CollectionUtil;

/**
 * <p>
 * 条件校验
 * </p>
 *
 * @author migo
 * @since 2021/11/3
 */
public class ConditionExpressionValidator {
    /**
     * 检查条件中的值
     * @param conditionNode
     * @throws XmsException
     */
    public static void checkValue(ConditionExpression conditionNode) throws XmsException {
        switch (conditionNode.Operator) {
            case ConditionOperator.EqualUserId:
                break;

            case ConditionOperator.NotEqualUserId:
                break;

            case ConditionOperator.EqualBusinessId:
                break;

            case ConditionOperator.NotEqualBusinessId:
                break;

            case ConditionOperator.EqualOrganizationId:
                break;

            case ConditionOperator.NotEqualOrganizationId:
                break;

            case ConditionOperator.Equal:
            case ConditionOperator.NotEqual:
            case ConditionOperator.BeginsWith:
            case ConditionOperator.DoesNotBeginWith:
            case ConditionOperator.DoesNotContain:
            case ConditionOperator.DoesNotEndWith:
            case ConditionOperator.EndsWith:
            case ConditionOperator.GreaterEqual:
            case ConditionOperator.GreaterThan:
            case ConditionOperator.LessEqual:
            case ConditionOperator.LessThan:
                if (CollectionUtil.isEmpty(conditionNode.Values) || conditionNode.Values.get(0) == null) {
                    raiseError(String.format(String.format("过滤条件中 [%s] 的值不能为空", conditionNode.AttributeName)));
                }
                break;

            case ConditionOperator.LastYear:
            case ConditionOperator.LastMonth:
            case ConditionOperator.LastWeek:
            case ConditionOperator.Last7Days:
                break;

            case ConditionOperator.LastXDays:
            case ConditionOperator.LastXHours:
            case ConditionOperator.LastXMonths:
            case ConditionOperator.LastXWeeks:
            case ConditionOperator.LastXYears:
                if (CollectionUtil.isEmpty(conditionNode.Values) || conditionNode.Values.get(0) == null) {
                    raiseError(String.format(String.format("过滤条件中 [%s] 的值不能为空", conditionNode.AttributeName)));
                }
                break;

            case ConditionOperator.NextMonth:
            case ConditionOperator.NextYear:
            case ConditionOperator.NextWeek:
            case ConditionOperator.Next7Days:
            case ConditionOperator.Today:
                break;

            case ConditionOperator.NextXDays:
            case ConditionOperator.NextXHours:
            case ConditionOperator.NextXMonths:
            case ConditionOperator.NextXWeeks:
            case ConditionOperator.NextXYears:
                if (CollectionUtil.isEmpty(conditionNode.Values) || conditionNode.Values.get(0) == null) {
                    raiseError(String.format(String.format("过滤条件中 [%s] 的值不能为空", conditionNode.AttributeName)));
                }
                break;

            case ConditionOperator.NotBetween:
            case ConditionOperator.Between:
                if (CollectionUtil.isEmpty(conditionNode.Values) || conditionNode.Values.size() != 2
                        || conditionNode.Values.get(0) == null || conditionNode.Values.get(1) == null) {
                    raiseError(String.format(String.format("过滤条件中 [%s] 的值应提供两个", conditionNode.AttributeName)));
                }
                break;

            case ConditionOperator.OlderThanXMonths:
            case ConditionOperator.On:
            case ConditionOperator.NotOn:
            case ConditionOperator.OnOrAfter:
            case ConditionOperator.OnOrBefore:
            case ConditionOperator.After:
            case ConditionOperator.Before:
                if (CollectionUtil.isEmpty(conditionNode.Values) || conditionNode.Values.get(0) == null) {
                    raiseError(String.format(String.format("过滤条件中 [%s] 的值不能为空", conditionNode.AttributeName)));
                }
                break;

            case ConditionOperator.OnOrAfterToday:
            case ConditionOperator.OnOrBeforeToday:
            case ConditionOperator.AfterToday:
            case ConditionOperator.BeforeToday:
            case ConditionOperator.ThisMonth:
            case ConditionOperator.ThisWeek:
            case ConditionOperator.ThisYear:
            case ConditionOperator.Tomorrow:
            case ConditionOperator.Yesterday:
                break;

            case ConditionOperator.OlderThanXYears:
            case ConditionOperator.OlderThanXDays:
            case ConditionOperator.AfterXYears:
            case ConditionOperator.AfterXMonths:
            case ConditionOperator.AfterXDays:
            case ConditionOperator.NotIn:
            case ConditionOperator.In:
            case ConditionOperator.Like:
            case ConditionOperator.Contains:
            case ConditionOperator.NotLike:
                if (CollectionUtil.isEmpty(conditionNode.Values) || conditionNode.Values.get(0) == null) {
                    raiseError(String.format(String.format("过滤条件中 [%s] 的值不能为空", conditionNode.AttributeName)));
                }
                break;

            case ConditionOperator.Null:
            case ConditionOperator.NotNull:
                break;

            default:
                break;
        }
    }

    private static void raiseError(String message) throws XmsException {
        throw new XmsException(message);
    }
}
